package com.it.myd.controller.main.module;

import com.it.myd.bean.box.Article;
import com.it.myd.bean.box.MsgBoard;
import com.it.myd.bean.box.Record;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.service.main.date.RecordService;
import com.it.myd.service.main.module.article.ArticleService;
import com.it.myd.service.main.module.msgboard.MsgBoardService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.Part;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/module/article")
public class ArticleController {

    @Autowired
    private ArticleService artService;
    @Autowired
    private RecordService recService;
    @Autowired
    private MsgBoardService msgBoardService;

    @RequestMapping("/uploadImage")
    @ResponseBody
    public Map<String, Object> uploadImage(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        Part part = request.getPart("file");
        String filename=part.getSubmittedFileName();
//        String filePath="E:/mydream/img/articleimg/"+user.getUser_id();
        String filePath="/usr/myd/img/articleimg/"+user.getUser_id();
        File uploadedFile= new File(filePath);
        if(!uploadedFile.exists()){
            uploadedFile.mkdir();
        }
        //上传文件到指定目录
        part.write(filePath+"/"+filename);
        Map<String, Object> ret = new HashMap<>();
        ret.put("location", "/articleimg/"+user.getUser_id()+"/"+filename);
        return ret;
    }

    @RequestMapping("/submit")
    @ResponseBody
    public ReturnValue submit(HttpServletRequest request) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String btnr =request.getParameter("btnr");
        String wznr =request.getParameter("wznr");
        String form_state =request.getParameter("form_state");

        Part part = request.getPart("fmnr");
        String filename=part.getSubmittedFileName();
//        String filePath="E:/mydream/img/articleimg/"+user.getUser_id();
        String filePath="/usr/myd/img/articleimg/"+user.getUser_id();
        File uploadedFile= new File(filePath);
        if(!uploadedFile.exists()){
            uploadedFile.mkdir();
        }
        part.write(filePath+"/"+filename);

        int id = artService.getArticleId();
        com.it.myd.bean.main.File file = new com.it.myd.bean.main.File();
        file.setFile_type(10002);
        file.setFile_path("/articleimg/"+user.getUser_id()+"/");
        file.setFile_name(filename);
        file.setFile_id(id);
        artService.InsertUserProFile(file);

        Article article = new Article();
        article.setArt_content(wznr);
        article.setUser(user);
        article.setArt_state(Integer.parseInt(form_state));
        article.setArt_type(1);
        article.setArt_title(btnr);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        article.setCreatetime(sdf.format(System.currentTimeMillis()));
        artService.insertArticle(article);
        ReturnValue ret = new ReturnValue();
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("unEffect")
    @ResponseBody
    public ReturnValue unEffect(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String rec_type = request.getParameter("rec_type");
        String obj_id = request.getParameter("obj_id");
        Record rec = new Record();
        rec.setObj_id(Integer.parseInt(obj_id));
        rec.setActiveman(String.valueOf(user.getUser_id()));
        recService.unEffect(rec);
        Article article = new Article();
        article.setArt_id(Integer.parseInt(obj_id));
        if(rec_type.equals("artupcount")){
            artService.subUpCount(article);
        }else{
            artService.subDownCount(article);
        }
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("addRecord")
    @ResponseBody
    public ReturnValue addRecord(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String rec_type = request.getParameter("rec_type");
        String obj_id = request.getParameter("obj_id");
        String passive_id = request.getParameter("passive_id");
        Record rec = new Record();
        rec.setRec_type(rec_type);
        rec.setObj_id(Integer.parseInt(obj_id));
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rec.setRecordtime(sdf.format(System.currentTimeMillis()));
        rec.setActiveman(String.valueOf(user.getUser_id()));
        rec.setPassiveman(passive_id);
        recService.addRecord(rec);
        Article article = new Article();
        article.setArt_id(Integer.parseInt(obj_id));
        if(rec_type.equals("artupcount")){
            artService.addUpCount(article);
        }else{
            artService.addDownCount(article);
        }
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("/getArticle")
    public String getArticle(HttpServletRequest request,Model model) throws ServletException, IOException {
        String art_id = request.getParameter("art_id");
        Article article = new Article();
        article.setArt_id(Integer.parseInt(art_id));

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        article.setUser(user);
        article = artService.selectArticle(article);
        List<MsgBoard> msgBoards = new ArrayList<>();
        MsgBoard mB= new MsgBoard();
        mB.setUser(user);
        mB.setMsg_ordid(Integer.parseInt(art_id));
        mB.setMsg_type(62000);
        msgBoards = msgBoardService.GetMsgBoards(mB);
        model.addAttribute("msgBoards",msgBoards);
        model.addAttribute("article",article);
        return "main/module/article/article";
    }

    @RequestMapping("addMsgBoard")
    @ResponseBody
    public ReturnValue addMsgBoard(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String msg_content = request.getParameter("msg_content");
        String msg_parid = request.getParameter("msg_parid");
        String msg_ordid = request.getParameter("msg_ordid");
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setUser(user);
        msgBoard.setMsg_content(msg_content);
        if(msg_parid!=null && !msg_parid.equals("")){
            msgBoard.setMsg_parid(Integer.parseInt(msg_parid));
            msgBoard.setMsg_istop(0);
        }else{
            msgBoard.setMsg_istop(1);
        }
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        msgBoard.setMsg_time(sdf.format(System.currentTimeMillis()));
        msgBoard.setMsg_type(62000);
        msgBoard.setMsg_ordid(Integer.parseInt(msg_ordid));
        msgBoardService.addMsgBoard(msgBoard);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess("发送成功");
        return returnValue;
    }
}
